package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"perfect-admin/global/response"
	"perfect-admin/model/request"
	"perfect-admin/service"
	"perfect-admin/utils"
	"time"
)

func UpdateCasbin(c *gin.Context) {
	var cmr request.CasbinInReceive
	if err := c.ShouldBindJSON(&cmr); err != nil{
		response.Response(c, 201, err.Error(), nil)
		return
	}
	AuthorityIdVerifyErr := utils.Verify(cmr, utils.CustomizeMap["AuthorityIdVerify"])
	if AuthorityIdVerifyErr != nil {
		response.Response(c, 202, AuthorityIdVerifyErr.Error(), nil)
		return
	}
	err := service.UpdateCasbin(cmr)
	if err != nil {
		response.Response(c, 203, fmt.Sprintf("添加规则失败，%v", err), nil)
	} else {
		response.Response(c, http.StatusOK, "添加规则成功", nil)
	}
	return
}

func GetPolicyPathByRoleId(c *gin.Context) {
	role_id := c.Query("role_id")
	if role_id == "" {
		response.Response(c, 201, "参数接收有误", nil)
		return
	}
	AuthorityIdVerifyErr := utils.Verify(request.CasbinInReceive{RoleId:role_id}, utils.CustomizeMap["AuthorityIdVerify"])
	if AuthorityIdVerifyErr != nil {
		response.Response(c, 201, AuthorityIdVerifyErr.Error(), nil)
		return
	}
	paths := service.GetPolicyPathByRoleId(role_id)
	response.Response(c, http.StatusOK, "获取角色Api成功", paths)
}

func CasbinTest(c *gin.Context)  {
	for i := 0; i < 100; i++{
		go sleep(i)
	}
	response.Response(c, http.StatusOK, "测试成功返回", nil)
}

func sleep(i int)  {
	fmt.Println(i)
	time.Sleep(1 * time.Second)
}